package com.xls.commonlibrary.ad.adn.reward.util;

import android.annotation.SuppressLint;
import android.webkit.ValueCallback;

import androidx.appcompat.app.AppCompatActivity;

import com.hjq.toast.Toaster;
import com.xls.commonlibrary.bean.ShowEcpmResult;
import com.xls.commonlibrary.http.Api;
import com.xls.commonlibrary.http.RetrofitHelper;
import com.xls.commonlibrary.http.RxLifecycle;
import com.xls.commonlibrary.http.Transformer;
import com.xls.commonlibrary.util.SysDataUtil;
import com.xls.commonlibrary.util.dislogx.DialogXUtil;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Nonnull;

import io.reactivex.rxjava3.annotations.NonNull;

public class PointUtil {// 获取积分工具
    private static volatile PointUtil singleton = null;

    private PointUtil() {
    }

    public static PointUtil getInstance() {
        if (singleton == null) {
            synchronized (PointUtil.class) {
                if (singleton == null) {
                    singleton = new PointUtil();
                }
            }
        }
        return singleton;
    }

    ValueCallback<ShowEcpmResult> mCallback = null;

    public void getPoints(
            String transId,
            @Nonnull AppCompatActivity activity,
            @NonNull ValueCallback<ShowEcpmResult> callback
    ) {
        this.mCallback = callback;
        getPoints(activity, transId);
    }


    @SuppressLint("CheckResult")
    public void getPoints(@Nonnull AppCompatActivity activity, String transId) {
        // 请求积分成功后，post给游戏页面 弹框当次积分和积分总和
        // todo 可能涉及到轮询的问题


        Map<String, String> p = new HashMap<>();
        p.put("appName", SysDataUtil.getInstance().getPkName());
        p.put("transId", transId);

        RetrofitHelper.getInstance().create(Api.class)
                .showEcpm(p)
                .compose(Transformer.transform())
                .compose(RxLifecycle.bindRxLifecycle(activity))
                .subscribe(
                        result -> {
                            if (result.getCode() == 1) {

                                // 两种回调选一个
                                if (this.mCallback != null) {
                                    this.mCallback.onReceiveValue(result.getData());
                                }

                                // todo 通知有问题，需要改为callback方式
//                                EventBus.getDefault().post(new MessageEvent(
//                                        EVENT_AD_GET_POINT_DATA,
//                                        "",
//                                        result.getData().getScore(),
//                                        result.getData().getCurrentScore()
//                                ));


                            } else {
                                DialogXUtil.closeLoading();
                                Toaster.show(result.getMsg());

                            }
                        },
                        throwable -> {
                            DialogXUtil.closeLoading();
                        }
                );
    }
}
